 
*introfiles
foreach t of numlist 2005/2010{
	clear
	use "S:\Project\Data\Intro_ordning\introord_`t'.dta"
	g intro`t' = 1
	g knrintro`t' = kommune
	g bydintro`t' = bydel
	qui tab inntekt,g(innt_)
	rename innt_1 intropdwrk`t'
	g norsk`t' = norskopplaring
	g norpraksis`t' = sprakpraksis
	g godkjutd`t' = godkjenning_utd
	g wrkpraksis`t' = arb_praksis
	g kurs`t' = kurs_komm
	g introstatus`t' = status_utgang_aar
	replace wrkpraksis`t' = arb_off if arb_off!="" & wrkpraksis`t'==""
	replace wrkpraksis`t' = arb_priv if arb_priv!="" & wrkpraksis`t'==""
	replace wrkpraksis`t' = yrkesproving if yrkesproving!="" & wrkpraksis`t'==""
	g startdate`t' = startdato
	keep lopenr intro`t' knrintro`t' bydintro`t' intropdwrk`t' norsk`t' norpraksis`t' godkjutd`t' wrkpraksis`t' kurs`t' introstatus`t' startdate`t'
	duplicates tag lopenr, g(dup)
	drop if dup!=0
	drop dup
	save intro`t'.dta, replace
}

foreach t of numlist 2011{
	clear
	use "S:\Project\Data\Intro_ordning\introord_`t'.dta"
	g intro`t' = 1
	g knrintro`t' = kommune
	g bydintro`t' = bydel
	qui tab inntekt,g(innt_)
	rename innt_1 intropdwrk`t'
	g norsk`t' = norskopplaring
	g norpraksis`t' = sprakpraksis
	g godkjutd`t' = godkjenning_utd
	g wrkpraksis`t' = arb_praksis
	g kurs`t' = kurs_komm
	g introstatus`t' = status_utgang_aar
	replace wrkpraksis`t' = arb_off if arb_off!="" & wrkpraksis`t'==""
	replace wrkpraksis`t' = yrkesproving if yrkesproving!="" & wrkpraksis`t'==""
	g startdate`t' = startdato
	keep lopenr intro`t' knrintro`t' bydintro`t' intropdwrk`t' norsk`t' norpraksis`t' godkjutd`t' wrkpraksis`t' kurs`t' introstatus`t' startdate`t'
	duplicates tag lopenr, g(dup)
	drop if dup!=0
	drop dup
	save intro`t'.dta, replace
}

foreach t of numlist 2012/2016{
	clear
	use "S:\Project\Data\Intro_ordning\introord_`t'.dta"
	g intro`t' = 1
	g knrintro`t' = kommune
	g bydintro`t' = bydel
	qui tab inntekt,g(innt_)
	rename innt_1 intropdwrk`t'
	g norsk`t' = norskopplaring
	g norpraksis`t' = sprakpraksis
	g godkjutd`t' = godkjenning_utd
	g wrkpraksis`t' = arb_praksis
	g kurs`t' = kurs_komm
	g introstatus`t' = status_utgang_aar
	replace wrkpraksis`t' = arb_off if arb_off==1 & wrkpraksis`t'!=1
	replace wrkpraksis`t' = yrkesproving if yrkesproving==1 & wrkpraksis`t'!=1
	g startdate`t' = startdato
	keep lopenr intro`t' knrintro`t' bydintro`t' intropdwrk`t' norsk`t' norpraksis`t' godkjutd`t' wrkpraksis`t' kurs`t' introstatus`t' startdate`t'
	duplicates tag lopenr, g(dup)
	drop if dup!=0
	drop dup
	save intro`t'.dta, replace
}


 

*voterfiles
foreach t of numlist 2013 2015 2017 2019{
    clear
	use "S:\Project\Data\Valgdata\manntall_`t'.dta"
	g vote`t' = Stemt_SSB
	keep lopenr vote`t'
	duplicates tag lopenr, g(dup)
	drop if dup!=0
	drop dup
	save vote`t'.dta, replace
}

*income  
foreach t of numlist 2000/2018{
	clear
	use "S:\Project\Data\Inntekt\inntekt`t'.dta"
	g wlonn`t' = wlonn
	g sosial`t' = sosial
	keep lopenr wlonn`t' sosial`t'
	duplicates tag lopenr, g(dup)
	drop if dup!=0
	drop dup
	save inntekt`t'.dta, replace
}

*disability
foreach t of numlist 2000/2018{
	clear
	use "S:\Project\Data\Inntekt\inntekt`t'.dta"
	g disability`t' = kode218 + sum_arbavkl
	keep lopenr disability`t'
	duplicates tag lopenr, g(dup)
	drop if dup!=0
	drop dup
	save disability`t'.dta, replace
}

*household
foreach t of numlist 2005 2006 2007 2008 2009 2010 2011 2014 2015 2016 2017 2018{
	clear
	use "S:\Project\Data\Hush_fam\hush_fam_`t'.dta"
	g famtype`t' = famtype
	g parstatus`t' = parstatus
	g komnr`t' = kommnr
	g bydel`t' = bydel
	keep lopenr famtype`t' parstatus`t' komnr`t' bydel`t'
	duplicates tag lopenr, g(dup)
	drop if dup!=0
	drop dup
	save hush`t'.dta, replace
}

foreach t of numlist 2012 2013{
	clear
	use "S:\Project\Data\Hush_fam\hush_fam_`t'.dta"
	g famtype`t' = famtype
	g parstatus`t' = parstatus
	g komnr`t' = KOMMNR
	g bydel`t' = bydel
	keep lopenr famtype`t' parstatus`t' komnr`t' bydel`t'
	duplicates tag lopenr, g(dup)
	drop if dup!=0
	drop dup
	save hush`t'.dta, replace
}

*education
clear
use "S:\Project\Data\Utdanning\utdanning.dta"
foreach t of numlist 2000/2019{
	preserve
	keep if aar==`t'
	g bu`t' = BU
	keep lopenr bu`t'
	duplicates tag lopenr, g(dup)
	drop if dup!=0
	drop dup
	save bu`t'.dta, replace
	restore
}

*civil status
use "S:\Project\Data\Hush_fam\sivilstand_2000_2019.dta"
foreach t of numlist 2000/2019{
	preserve
	keep if aar==`t'
	g civstatus`t' = sivilstand
	keep lopenr civstatus`t'
	duplicates tag lopenr, g(dup)
	drop if dup!=0
	drop dup
	save civstatus`t'.dta, replace
	restore
}

*residence
use "S:\Project\Data\Befolkning\bosted_2000_2019.dta"
foreach t of numlist 2000/2019{
	preserve
	keep if aar==`t'
	g grkrets`t' = grunnkrets
	keep lopenr grkrets`t'
	duplicates tag lopenr, g(dup)
	drop if dup!=0
	drop dup
	save grkrets`t'.dta, replace
	restore
}

use "S:\Project\Data\Befolkning\bosted_2020_2022.dta"
foreach t of numlist 2020/2022{
	preserve
	keep if aar==`t'
	g grkrets`t' = grunnkrets
	keep lopenr grkrets`t'
	duplicates tag lopenr, g(dup)
	drop if dup!=0
	drop dup
	save grkrets`t'.dta, replace
	restore
}

*citizenship
use "S:\Project\Data\Befolkning\statsborgerskap.dta"
foreach t of numlist 2000/2019{
	preserve
	keep if aar==`t'
	g citizenship`t' = regstatus
	keep lopenr citizenship`t'
	duplicates tag lopenr, g(dup)
	drop if dup!=0
	drop dup
	save citizenship`t'.dta, replace
	restore
}

*citizenship date
use "S:\Project\Data\Befolkning\statsborgerskap.dta"
duplicates tag lopenr, g(dup)
drop if dup!=0
keep if statsborgerskap==0
keep lopenr statsborgerskap stb_dato
rename statsborgerskap norcitizenship
rename  stb_dato date_citizen
save norcitizenship.dta, replace

*share norwegians
clear
use "S:\Project\Data\Befolkning\faste_oppl.dta"

foreach t of numlist 2000/2019{
	preserve
	merge 1:1 lopenr using "grkrets`t'.dta" 
	drop if grkrets`t'==""
	g norw = 0
	replace norw = 1 if fodeland=="000"
	collapse (mean) norw, by(grkrets`t')
	rename norw norw`t'
	save "norw`t'", replace
	restore
}

*partner
foreach t of numlist 2005/2018{
	clear
	use "S:\Project\Data\Hush_fam\hush_fam_`t'.dta"
	gen partner = .
	replace partner = 1 if parstatus=="1"
	replace partner = 1 if parstatus=="2"
	keep if partner==1
	drop if lopenr_hush==""
	bys lopenr_hush: g noobs = _n
	drop if noobs>2
	g lopenr_partner = lopenr_hush if lopenr!=lopenr_hush
	g no = 1 if lopenr_partner!=""
	replace no = 2 if lopenr_partner==""
	bys lopenr_hush : egen mean = mean(no)
	keep if mean==1.5
	egen husid = group(lopenr_hush)
	xtset husid no
	g lopenr_partner2 = lopenr if no==1
	bys husid: carryforward lopenr_partner2,gen(lopenr_partner3)
	replace lopenr_partner = lopenr_partner3 if lopenr_partner==""
	keep lopenr lopenr_hush  lopenr_partner 

	rename lopenr lopenr2
	rename lopenr_partner lopenr
	merge 1:1 lopenr using "S:\Project\Data\Befolkning\faste_oppl.dta"
	keep if _merge==3
	rename lopenr lopenr_partner
	rename lopenr2 lopenr
	rename landbak3gen landbak_partner
	keep lopenr lopenr_partner landbak_partner
	rename landbak_partner landbak_partner`t' 
	rename lopenr_partner lopenr_partner`t'
	save "partner`t'", replace
}


foreach t of numlist 2000/2018{
	clear
	use "S:\Project\Data\Fagforening\fagforening`t'.dta"
	duplicates tag lopenr, g(dup)
	drop if dup!=0
	g union`t' = 1
	keep lopenr union`t'
	save union`t'.dta, replace
}

*household size
foreach t of numlist 2005/2018{
	clear
	use "S:\Project\Data\Hush_fam\hush_fam_`t'.dta"
	drop if lopenr_hush == ""
	bys lopenr_hush: g hhsize`t' = _N
	duplicates tag lopenr, g(dup)
	drop if dup!=0
	drop dup
	g lopenr_hush`t' = lopenr_hush
	keep lopenr lopenr_hush`t' hhsize`t'
	save "hholdstr`t'", replace
}

*birth year first child
clear
use "S:\Project\Data\Befolkning\faste_oppl.dta"
keep  lopenr foedselsaar
rename lopenr lnr_barn_01
save lnr_barn01_faar, replace

clear
use "S:\Project\Data\Hush_fam\slekt.dta"
keep lopenr lnr_barn_01
duplicates tag lopenr, g(dup)
drop if dup!=0
drop dup
duplicates tag lnr_barn_01, g(dup)
drop if dup!=0
drop dup
merge 1:1 lnr_barn_01 using lnr_barn01_faar
keep if _merge == 3
drop _merge
rename foedselsaar yrfirstbrn
save yrfirstbrn, replace

*creating analysis file
clear
use "S:\Project\Data\Befolkning\faste_oppl.dta"
keep if igrus3!=""


foreach t of numlist 2005/2016{
	merge 1:1 lopenr using "intro`t'.dta", nogen
	keep if igrus3!=""
}




foreach t of numlist 2013 2015 2017 2019{
	merge 1:1 lopenr using "vote`t'.dta", nogen
	keep if igrus3!=""
}



foreach t of numlist  2000/2018{
	merge 1:1 lopenr using "inntekt`t'.dta", nogen
	keep if igrus3!=""
}



foreach t of numlist  2000/2019{
	merge 1:1 lopenr using "bu`t'.dta", nogen
	keep if igrus3!=""
}



foreach t of numlist  2005/2018{
	merge 1:1 lopenr using "hush`t'.dta", nogen
	keep if igrus3!=""
}

foreach t of numlist  2000/2019{
	merge 1:1 lopenr using "civstatus`t'.dta", nogen
	keep if igrus3!=""
}

foreach t of numlist  2000/2019{
	merge 1:1 lopenr using "citizenship`t'.dta", nogen
	keep if igrus3!=""
}

foreach t of numlist  2000/2019{
	merge 1:1 lopenr using "grkrets`t'.dta", nogen
	keep if igrus3!=""
}



merge m:m lopenr using norcitizenship.dta, nogen
keep if igrus3!=""


foreach t of numlist  2000/2019{
	merge m:1 grkrets`t' using "norw`t'.dta", nogen
}

foreach t of numlist  2005/2018{
	merge m:1 lopenr using "partner`t'.dta", nogen
	keep if igrus3!=""
}

foreach t of numlist  2000/2018{
	merge 1:1 lopenr using "union`t'.dta", nogen
	keep if igrus3!=""
}

save analysis, replace